<?php
/**
 * 后台管理员数据管理
 */
namespace logic\admin;
class AdminManage{
    
    private $User;  //操作人员对象
    public function __construct(\logic\user\User $User){
        $this->User = $User;
    }
    
    
    /**
     * 读取一个管理员信息
     */
    public function find($param){
        return D('Admin','admin')->find($param);
    }
    
    /**
     * 新增一个管理员 
     */ 
    public function add($data){
        $checkAccount = D('Admin','admin')->find(['account'=>$data['account']]);
        if( $checkAccount ) logicE('账户已经存在');
        if( $data['confirmpwd'] != $data['password'] )  logicE('两次密码不一致');
        $info = $this->User->getInfo();
        if( $info['type'] != 1 ) logicE('您无权限添加管理员');
        $data['agentid'] = $info['agentid'];
        
        $check = [
            'agentid'   => ['check'=>'must','msg'=> '代理商ID不能为空','reg'=>'int'],
            'status'    => ['def'=>1],
            //'type'      => ['reg'=>[1,2,3]],
            'account'   => ['check'=>'must','msg'=>'账号不能为空','reg'=>'account'],
            'password'  => ['check'=>'must','msg'=>'密码不能为空'],
            'name'      => '',
            'mobile'    => ['reg'=> 'mobile'],
            'roleid'    => ['check'=>'must','msg'=> '角色不能为空','reg'=>'int'],
        ];
        
        $save = pVerify($check,$data);        
        $despassword = self::desPassword($save['password']);
        $save['password'] = $despassword['password'];
        $save['passsalt'] = $despassword['passsalt'];
        return D('Admin','admin')->add($save);
    }    
    
    /**
     * 修改用户状态
     */
    public function editStatus($adminid){
        $info = D('Admin','admin')->find(['adminid'=>$adminid]);
        $data['status'] = $info['status']==1?2:1;
        return D("Admin","admin")->edit($adminid,$data);
    }
    
    /**
     * 修改一个管理员信息
     */
    public function edit($adminid, $data){
        $check = [
            'status'    => ['reg'=>[1,2]],
           // 'account'   => ['check'=>'must','msg'=>'账号不能为空','reg'=>'account'],
            'name'      => '',
            'mobile'    => ['reg'=> 'mobile'],
            'roleid'    => ['reg'=>'int'],
        ];
        if( isset($data['account'])){
            $checkAccount = D('Admin','admin')->find(['account'=>$data['account']]);
            if($checkAccount && $checkAccount['adminid'] != $adminid ) logicE('该账户已经存在,不能重复');   
            $check['account'] = ['check'=>'must','msg'=>'账号不能为空','reg'=>'account'];
        }  
        
        $save = pVerify($check,$data);
        if( $data['password'] ){
            if( $data['confirmpwd'] != $data['password'] )  logicE('两次密码不一致');            
            $despassword = self::desPassword($data['password']);
            $save['password'] = $despassword['password'];
            $save['passsalt'] = $despassword['passsalt'];
        }        
        return D("Admin","admin")->edit($adminid,$save);
    }
    
    /**
     * 列表读取
     */
    public function getList($param){
        if( $param['keyword'] ){
            if( \core\helper\Verify::check($param['keyword'],'mobile')){
                $param['mobile'] = (string)$param['keyword'];
                unset($param['keyword']);
            }      
        }
        $data = D('AdminList','admin')->getList($param);        
        $loginLog = D("LoginLogs","admin")->getLastLoginList(["userid"=>array_key_column($data['list'],'adminid')]);
        foreach($data['list'] as &$account){
            $account['lastlogintime'] = '';
            $account['logins']  = 0;
            $account['ip']  = '';
            if($loginLog){
                foreach($loginLog as $log){
                    if($log['userid']!=$account['adminid'])continue;
                    $info = D("LoginLogs","admin")->getLastLogin($account['adminid']);
                    $account['logins'] = $log['logins'];
                    $account['ip'] = $info['ip'];
                    $account['lastlogintime'] = $info['addtime'];
                }
            }
        }
        return $data;
    }
    
    /**
     * 对密码进行加密
     * @param $password 单次md5之后的密码或原始密码
     * @param $passsalt 密码撒盐
     * @param $passwordHasMd5 密码是否已经md5 过
     */
    public static function desPassword($password,$passsalt = '',$passwordHasMd5 = false ){
        $key = 'ko$t4Ko1~0mso';
        if( !$passwordHasMd5 ) $password = md5($password);
        if( !$passsalt ) $passsalt = \core\helper\Tools::rand(6);
        trim($password);
        trim($passsalt);
        return [
            'password'  =>  md5( $password.$passsalt.$key ),
            'passsalt'  => $passsalt
        ];
    }
    
    
    
}